package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.UserLoginService;
import com.mall.user.UserLoginToken;
import com.mall.user.bo.UserLoginBO;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.UserConverterMapper;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dto.*;
import com.mall.user.utils.AESUtil;
import com.mall.user.utils.ExceptionProcessorUtils;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
import java.util.Map;

/**
 * @description: ${description}
 * @author: cuicui
 **/
@Slf4j
@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    UserConverterMapper userConverterMapper;
    @Autowired
    MemberServiceImpl memberService;
    @Override
    public VerifyTokenResponse verifyToken(String token) {
        VerifyTokenResponse verifyTokenResponse = new VerifyTokenResponse();
        try {
            String freeToken = JwtTokenUtils.builder().token(token).build().freeJwt();
            Map<String,Object> map = (Map) JSON.parseObject(freeToken);
            String username = (String) map.get("username");
            Integer userId = (Integer) map.get("uid");
            verifyTokenResponse.setUsername(username);
            verifyTokenResponse.setUid(userId.longValue());
        } catch (ValidateException e) {
            verifyTokenResponse.setMsg(e.getMessage());
            verifyTokenResponse.setCode(e.getErrorCode());
            return verifyTokenResponse;
        }
        verifyTokenResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        verifyTokenResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return verifyTokenResponse;
    }

    @Override
    public UserLoginResponse login( UserLoginBO bo) {
        UserLoginRequest userLoginRequest = userConverterMapper.userLoginBO2Request(bo);
        UserLoginResponse response = new UserLoginResponse();
        try {
            //参数校验
            userLoginRequest.requestCheck();
            //将参数中的密码进行加密
            String password = new AESUtil(bo.getUserPwd()).encrypt();
            List<Member> members = memberService.queryMemberByUsernameAndPwd(bo.getUserName(), password);
            if(members.size()==0){
                //通过用户名查询数据库中用户信息，不存在
                response.setCode(SysRetCodeConstants.USERORPASSWORD_ERRROR.getCode());
                response.setMsg(SysRetCodeConstants.USERORPASSWORD_ERRROR.getMessage());
                return response;
            }
            //判断用户是否激活
            if (members.get(0).getIsVerified().equals("N")) {
                response.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
                response.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
                return response;
            }
            response = userConverterMapper.converter( members.get(0));
            UserLoginToken userLoginToken = new UserLoginToken(response.getId().intValue(),response.getUsername());
            String mag = JSON.toJSONString(userLoginToken);
            String token = JwtTokenUtils.builder().msg(mag).build().creatJwtToken();
            response.setToken(token);
        } catch (ValidateException e) {
            //参数不合法
            e.printStackTrace();
            response.setCode(e.getErrorCode());
            response.setMsg(e.getMessage());
        }
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }
}
